#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <unistd.h>
#include "Thread.hpp"
#include "Task.hpp"
#include "LockGuard.hpp"

void print(std::string name)
{
    std::cout << name << "is running..." << std::endl;
}

int ticket = 10000;

// pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

void route(ThreadMoudle::ThreadData *td)
{
    while (true)
    {
        LockGuard lock(td->_mutex);
        if (ticket > 0)
        {
            usleep(1);
            std::cout << td->_name << " ,ticket : " << ticket << std::endl;
            // std::cout << td->_name << " " << &td->_mutex << std::endl;
            ticket--;
        }
        else
        {
            break;
        }
    }
}

int main()
{
    srand(time(nullptr));
    pthread_mutex_t mutex;
    pthread_mutex_init(&mutex, nullptr);
    std::vector<ThreadMoudle::Thread<Task>> Threads;
    for (int i = 0; i < 4; i++)
    {
        int x = rand() % 10, y = rand() % 10;
        std::string name = "thread " + std::to_string(i + 1);
        // Threads.emplace_back(name, route, &mutex);
        Threads.emplace_back(name, Task(x, y), &mutex);
    }

    for (int i = 0; i < 4; i++)
    {
        Threads[i].start();
    }

    sleep(100);

    for (int i = 0; i < 4; i++)
    {
        Threads[i].stop();
        Threads[i].join();
    }

    return 0;
}
